package com.cloud.wms.ui.group.sampling;


import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cloud.core.eventBus.Event;
import com.cloud.core.utils.TimeUtils;
import com.cloud.widget.ScanText;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarActivity;
import com.cloud.wms.config.EventConfig;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.group.contract.GroupSamplingContract;
import com.cloud.wms.ui.group.entity.BatchCountEntity;
import com.cloud.wms.ui.group.entity.SamplingCommodityEntity;
import com.cloud.wms.ui.group.presenter.GroupSamplingPresenter;
import com.cloud.wms.ui.main.entity.TaskEntity;
import com.cloud.wms.ui.sort.entity.BatchInfoEntity;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * @Author:Chen
 * @Date: 2021-03-01 16:51
 * @Description:
 */
public class SamplingOrderActivity extends BaseBarActivity<GroupSamplingPresenter> implements GroupSamplingContract.View {

    @BindView(R.id.scan_text)
    ScanText scanText;
    @BindView(R.id.tv_need_count)
    TextView tvNeedCount;
    @BindView(R.id.tv_sampling_count)
    TextView tvSamplingCount;
    @BindView(R.id.ll_count)
    LinearLayout llCount;
    private BatchInfoEntity batchInfoEntity;
    private TaskEntity taskEntity;
    private String carryCode;

    @Override
    protected String barTitle() {
        return "抽检订单";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_group_sampling_order;
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initEvent() {
        initScanText(scanText);
        taskEntity = getIntent().getParcelableExtra(IntentKey.TASK_KEY);
        batchInfoEntity = getIntent().getParcelableExtra(IntentKey.INFO_KEY);
        if (taskEntity != null)
            taskEntity.setBeginTime(TimeUtils.getCurrentDateAndTime());
        mPresenter.loadSamplingCount(taskEntity == null ? batchInfoEntity.getBatchID() : taskEntity.getBillID());
    }

    @Override
    protected void onScanHandle(String result, int resultCode) {
        super.onScanHandle(result, resultCode);
        scanText.addHistory(result);
        carryCode = result;
        mPresenter.loadSamplingCommodity(taskEntity == null ? batchInfoEntity.getBatchID() : taskEntity.getBillID(), result);
    }

    @Override
    public void onSuccess(Message message) {
        switch (message.what) {
            case GroupSamplingPresenter.SAMPLING_COUNT:
                BatchCountEntity countEntity = (BatchCountEntity) message.obj;
                tvNeedCount.setText(countEntity.getNeedSamplingNum());
                tvSamplingCount.setText(countEntity.getSamplingNum());
                llCount.setVisibility(View.VISIBLE);
                break;
            case GroupSamplingPresenter.BATCH_DETAIL:
                SamplingConfirmActivity.start(this, taskEntity, batchInfoEntity, tvNeedCount.getText().toString().trim(),
                        carryCode, ((ArrayList<SamplingCommodityEntity>) message.obj));
                break;
        }
    }

    @Override
    protected boolean isUseEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void onEventCome(Event event) {
        if (event == null) {
            return;
        }
        switch (event.getCode()) {
            case EventConfig.CLOSE_REFRESH:
                this.finish();
                break;
        }
    }

    public static void start(Context context, TaskEntity taskEntity, BatchInfoEntity batchInfoEntity) {
        Intent intent = new Intent(context, SamplingOrderActivity.class);
        intent.putExtra(IntentKey.TASK_KEY, taskEntity);
        intent.putExtra(IntentKey.INFO_KEY, batchInfoEntity);
        context.startActivity(intent);
    }
}
